<template>
  <div class="user-activity">
    <div class="post">
      <div class="user-block">
        <img
          class="img-circle"
          :src="'https://wpimg.wallstcn.com/57ed425a-c71e-4201-9428-68760c0537c4.jpg'+avatarPrefix"
        >
        <span class="username text-muted">{{ prescription.userInfoVO.username }}</span>
        <span class="description">年龄：{{ prescription.userInfoVO.age }}</span>
      </div>
    </div>

    <el-form
      class="bingli"
      :model="prescription"
    >
      <el-form-item label="病因">
        <el-input
          v-model="prescription.pathogeny"
          placeholder="请输入病因"
        />
      </el-form-item>
      <el-form-item label="服药周期">
        <el-date-picker
          v-model="prescription.medicationTimeStart"
          type="date"
          value-format="timestamp"
          placeholder="选择日期"
        />
        -----
        <el-date-picker
          v-model="prescription.medicationTimeEnd"
          type="date"
          value-format="timestamp"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="病例">
        <el-input
          v-model="prescription.remark"
          placeholder="请输入详细记录"
          type="textarea"
          rows="10"
        />
      </el-form-item>
      <el-form-item
        label="用药"
        class="editor-container"
      >
        <dnd-list
          ref="dnd"
          :list1="list1"
          :list2="list2"
          :level="prescription.userInfoVO.level"
          :prescription-id="prescriptionId"
          list1-title="开具的药品"
          list2-title="所有药品"
          width1="45%"
          width2="45%"
          @selectList2="selectList2"
        />
      </el-form-item>

      <div style="text-align:right;">
        <el-button
          type="danger"
          @click="haddleCancel"
        >取消</el-button>
        <el-button
          type="primary"
          @click="confirPrescription"
        >确定</el-button>
      </div>
    </el-form>
    <el-dialog
      class="select-medicine-dialog"
      :visible.sync="dialogMedicine"
      :title="'选择药品'"
      width="80%"
      append-to-body
    >
      <mediceSelect
        @cancel="medicineCancel"
        @confirm="selectMedicine"
      />
    </el-dialog>
    <el-dialog
      :visible.sync="dialogMedicineNumber"
      :title="'填写数量'"
      append-to-body
    >

      数量：
      <el-input
        v-model="number"
        placeholder="请输入药品数量"
        type="number"
        rows="10"
      />

      <div style="text-align:right;">
        <el-button
          type="primary"
          style="margin:20px 0"
          @click="confirmMedicine"
        >确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const avatarPrefix = '?imageView2/1/w/80/h/80'
const carouselPrefix = '?imageView2/2/h/440'
import { getPrescriptionInfoById, updatePresciption } from '@/api/prescription'
import { mapGetters } from 'vuex'
import { dateChange } from '@/utils/time'
import mediceSelect from '@/components/medicine'
import DndList from '@/components/DndList'
import { timestampToTime } from '@/utils/time'

const defaultPrescription = {
  pathogeny: '',
  day: '',
  type: '1',
  remake: '',
  adminId: '',
  userId: '',
  userInfoVO: {},
  medicationTimeStart: Date.parse(new Date()),
  medicationTimeEnd: Date.parse(new Date())
}

export default {
  components: {
    mediceSelect,
    DndList
  },
  props: {
    user: {
      type: Object,
      requied: true,
      default: null
    }
  },
  data() {
    return {
      carouselImages: [
        'https://wpimg.wallstcn.com/9679ffb0-9e0b-4451-9916-e21992218054.jpg'],
      avatarPrefix,
      carouselPrefix,
      dynamicTags: [],
      inputVisible: false,
      inputValue: '',
      prescription: Object.assign({}, defaultPrescription),
      dialogMedicine: false,
      dialogMedicineNumber: false,
      medicine: {},
      number: '',
      list1: [],
      list2: [],
      prescriptionId: ''
    }
  },
  computed: {
    ...mapGetters(['adminId'])
  },
  created() {
    this.getPrescription()
  },
  methods: {
    async getPrescription() {
      if (this.$route.params.prescriptionId !== ':prescriptionId') {
        this.prescriptionId = this.$route.params.prescriptionId.toString()
        const { data } = await getPrescriptionInfoById({ id: this.$route.params.prescriptionId })
        this.$refs.dnd.price = data.amount
        data.medicationTimeStart = data.medicationTimeStart === 0 ? null : data.medicationTimeStart
        data.medicationTimeEnd = data.medicationTimeEnd === 0 ? null : data.medicationTimeStart
        this.prescription = data
        this.list1 = data.prescriptionMedicineDTOList
      } else {
        this.$emit('showDialogUser')
      }
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
    },
    showInput() {
      this.dialogMedicine = true
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.dynamicTags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    haddleCancel() {
      this.$emit('cancel')
      this.$router.go(-1)
    },
    async confirPrescription() {
      this.prescription.adminId = this.adminId
      this.prescription.userId = this.user.id
      // this.medicationTimeStart = new Date().getTime()
      // this.medicationTimeEnd = dateChange(this.day, false)
      const data = await updatePresciption(this.prescription)
      // const id = data.data
      // let medicine = this.
      // prescriptionId = data.data
      // const { message2 } = await addPrescriptionMedicine(this.prescription)
      this.$notify({
        title: '提交成功',
        dangerouslyUseHTMLString: true,
        message: `
            <div>姓名: ${this.prescription.userInfoVO.username}</div>
            <div>消息: ${data.message}</div>
          `,
        type: 'success'
      })
      // this.prescription = { brand_right: 0 }
      this.$router.push('/user')
    },
    medicineCancel() {
      this.dialogMedicine = false
    },
    selectMedicine(data) {
      this.medicine = data
      this.dialogMedicine = false
      this.dialogMedicineNumber = true
    },
    confirmMedicine() {
      this.medicine.number = this.number
      this.dynamicTags.push(this.medicine)
      this.number = ''
      this.medicine = {}
      this.dialogMedicineNumber = false
    },
    selectList2(data) {
      this.list2 = data
    }
  }
}
</script>

<style lang="scss" scoped>
.user-activity {
  .user-block {
    .username,
    .description {
      display: block;
      margin-left: 50px;
      padding: 2px 0;
    }

    .username {
      font-size: 16px;
      color: #000;
    }

    :after {
      clear: both;
    }

    .img-circle {
      border-radius: 50%;
      width: 40px;
      height: 40px;
      float: left;
    }

    span {
      font-weight: 500;
      font-size: 12px;
    }
  }

  .post {
    font-size: 14px;
    border-bottom: 1px solid #d2d6de;
    margin-bottom: 15px;
    padding-bottom: 15px;
    color: #666;

    .image {
      width: 100%;
      height: 100%;
    }

    .user-images {
      padding-top: 20px;
    }
  }

  .list-inline {
    padding-left: 0;
    margin-left: -5px;
    list-style: none;

    li {
      display: inline-block;
      padding-right: 5px;
      padding-left: 5px;
      font-size: 13px;
    }

    .link-black {
      &:hover,
      &:focus {
        color: #999;
      }
    }
  }
}
.tian {
  width: 150px;
  margin-right: 10px;
}

.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
